package user

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
	"shangyuapi/service"
	"time"
)

type OrderaddBody struct {
	Uid         int64 `json:"uid" gorm:"uid"`       //生成订单人
	Userid      int64 `json:"userid" gorm:"userid"` //对方id
	Hongniangid int64 `json:"hongniangid" gorm:"hongniangid"`
}

func Shoudan(ctx *gin.Context) {
	request := OrderaddBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid

	var order models.Orderuser
	data_source.Db.Where("uid = ?", uid).First(&order)

	var price float64
	var shoudanprice float64
	shoudan := 1
	if order.ID > 0 {
		shoudan = 2
	}

	var confing models.Config
	data_source.Db.Where("id = 3").First(&confing)
	shoudanprice = float64(confing.Value)

	var fuwu models.Fuwu
	data_source.Db.Where("id = 2").First(&fuwu)
	price = fuwu.Price
	ctx.JSON(http.StatusOK, gin.H{
		"code":         0,
		"error":        0,
		"success":      200,
		"data":         shoudan,
		"price":        price,
		"shoudanprice": shoudanprice,
		"msg":          "ok",
	})
	return

}

func Orderadd(ctx *gin.Context) {
	request := OrderaddBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	userid := request.Userid
	hongniangid := request.Hongniangid

	var order models.Orderuser
	data_source.Db.Where("uid = ?", uid).First(&order)

	var orderuser models.Orderuser
	data_source.Db.Where("uid = ?", userid).First(&order)

	var uprice float64
	var userprice float64
	var yhprice float64
	var price float64
	var sdprice float64
	var dcprice float64

	var confing models.Config
	data_source.Db.Where("id = 3").First(&confing)
	sdprice = float64(confing.Value)
	var fuwu models.Fuwu
	data_source.Db.Where("id = 2").First(&fuwu)
	dcprice = fuwu.Price

	price = dcprice * 2
	if order.ID > 0 {
		uprice = sdprice
	} else {
		uprice = dcprice
	}
	if orderuser.ID > 0 {
		userprice = sdprice
	} else {
		userprice = dcprice
	}

	var user models.User
	data_source.Db.Where("id = ?", uid).First(&user)
	if user.Xingbi < uprice {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "星币不足",
		})
		return
	}

	yhprice = price - uprice - userprice

	var state int64
	if hongniangid == 0 {
		state = 2
	} else {
		state = 1
	}

	orderadd := models.Orders{
		Uid:         uid,
		Userid:      userid,
		Hongniangid: hongniangid,
		Status:      1,
		Addtime:     time.Now().UnixNano(),
		Uprice:      uprice,
		Userprice:   userprice,
		Fwprice:     uprice + userprice,
		Price:       price,
		Yhprice:     yhprice,
		State:       state,
		Fuwu:        "1v1约会",
		Upriceuid:   uid,
	}
	data_source.Db.Create(&orderadd)

	orderuseradd := models.Orderuser{
		Uid:     uid,
		Orderid: orderadd.ID,
	}
	data_source.Db.Create(&orderuseradd)

	data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("xingbi", user.Xingbi-uprice)
	xingbi := models.Userxingbi{
		Uid:     uid,
		Price:   uprice,
		Status:  2,
		Title:   "约会付款-付自己的",
		Addtime: time.Now().Unix(),
		Dqprice: user.Xingbi - uprice,
	}
	data_source.Db.Create(&xingbi)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type OrderslistBody struct {
	Uid      int64 `json:"uid" gorm:"uid"`
	ID       int64 `json:"id" gorm:"id"`
	State    int64 `json:"state" gorm:"state"`
	PageNo   int   `json:"pageNo"`
	Pagesize int   `json:"pagesize"`
	Cityid   int64 `json:"cityid"`
	Classify int64 `json:"classify"`
}

type OrderslistRBody struct {
	Data       []models.Orders `json:"data"`
	PageNo     int             `json:"pageNo"`
	PageSize   int             `json:"pageSize"`
	TotalCount int64           `json:"totalCount"`
	TotalPage  int64           `json:"totalPage"`
}

func Orderbuy(ctx *gin.Context) {
	request := OrderslistBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	id := request.ID
	state := request.State

	var orders models.Orders
	data_source.Db.Where("id = ?", id).First(&orders)
	var user models.User
	data_source.Db.Where("id = ?", uid).First(&user)
	if state == 1 {
		if orders.Uid == uid {
			if orders.Upriceuid > 0 {
				ctx.JSON(http.StatusOK, gin.H{
					"code":    400,
					"error":   1,
					"success": 400,
					"data":    "on",
					"msg":     "您已经付款了",
				})
				return
			} else {
				if orders.Uprice > user.Xingbi {
					ctx.JSON(http.StatusOK, gin.H{
						"code":    400,
						"error":   1,
						"success": 400,
						"data":    "on",
						"msg":     "星币不足",
					})
					return
				} else {
					if orders.Userpriceuid > 0 {
						orderupdata := models.Orders{
							Upriceuid: uid,
							Status:    4,
						}
						data_source.Db.Where("id = ?", id).Updates(&orderupdata)
					} else {
						orderupdata := models.Orders{
							Upriceuid: uid,
							Status:    3,
						}
						data_source.Db.Where("id = ?", id).Updates(&orderupdata)
					}
					data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("xingbi", user.Xingbi-orders.Uprice)
					xingbi := models.Userxingbi{
						Uid:     uid,
						Price:   orders.Uprice,
						Status:  2,
						Title:   "约会付款-付自己的",
						Addtime: time.Now().Unix(),
						Dqprice: user.Xingbi - orders.Uprice,
					}
					data_source.Db.Create(&xingbi)

				}
			}

		} else if orders.Userid == uid {

			if orders.Userpriceuid > 0 {
				ctx.JSON(http.StatusOK, gin.H{
					"code":    400,
					"error":   1,
					"success": 400,
					"data":    "on",
					"msg":     "您已经付款了",
				})
				return
			}
			if orders.Userprice > user.Xingbi {
				ctx.JSON(http.StatusOK, gin.H{
					"code":    400,
					"error":   1,
					"success": 400,
					"data":    "on",
					"msg":     "星币不足",
				})
				return
			} else {
				if orders.Upriceuid > 0 {
					orderupdata := models.Orders{
						Userpriceuid: uid,
						Status:       4,
					}
					data_source.Db.Where("id = ?", id).Updates(&orderupdata)
				} else {
					orderupdata := models.Orders{
						Userpriceuid: uid,
						Status:       3,
					}
					data_source.Db.Where("id = ?", id).Updates(&orderupdata)
				}
				data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("xingbi", user.Xingbi-orders.Userprice)
				xingbi := models.Userxingbi{
					Uid:     uid,
					Price:   orders.Userprice,
					Status:  2,
					Title:   "约会付款-付自己的",
					Addtime: time.Now().Unix(),
					Dqprice: user.Xingbi - orders.Userprice,
				}
				data_source.Db.Create(&xingbi)
			}

		}

	} else if state == 2 {
		if orders.Uid == uid {
			if orders.Userpriceuid > 0 {
				ctx.JSON(http.StatusOK, gin.H{
					"code":    400,
					"error":   1,
					"success": 400,
					"data":    "on",
					"msg":     "对方已经付款了",
				})
				return
			} else {
				if orders.Userprice > user.Xingbi {
					ctx.JSON(http.StatusOK, gin.H{
						"code":    400,
						"error":   1,
						"success": 400,
						"data":    "on",
						"msg":     "星币不足",
					})
					return
				} else {
					if orders.Upriceuid > 0 {
						orderupdata := models.Orders{
							Userpriceuid: uid,
							Status:       4,
						}
						data_source.Db.Where("id = ?", id).Updates(&orderupdata)
					} else {
						orderupdata := models.Orders{
							Userpriceuid: uid,
							Status:       3,
						}
						data_source.Db.Where("id = ?", id).Updates(&orderupdata)
					}
					data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("xingbi", user.Xingbi-orders.Userprice)
					xingbi := models.Userxingbi{
						Uid:     uid,
						Price:   orders.Userprice,
						Status:  2,
						Title:   "约会付款-付对方的",
						Addtime: time.Now().Unix(),
						Dqprice: user.Xingbi - orders.Userprice,
					}
					data_source.Db.Create(&xingbi)

				}
			}

		} else if orders.Userid == uid {
			if orders.Upriceuid > 0 {
				ctx.JSON(http.StatusOK, gin.H{
					"code":    400,
					"error":   1,
					"success": 400,
					"data":    "on",
					"msg":     "对方已经付款了",
				})
				return
			}
			if orders.Uprice > user.Xingbi {
				ctx.JSON(http.StatusOK, gin.H{
					"code":    400,
					"error":   1,
					"success": 400,
					"data":    "on",
					"msg":     "星币不足",
				})
				return
			} else {
				if orders.Userpriceuid > 0 {
					orderupdata := models.Orders{
						Upriceuid: uid,
						Status:    4,
					}
					data_source.Db.Where("id = ?", id).Updates(&orderupdata)
				} else {
					orderupdata := models.Orders{
						Upriceuid: uid,
						Status:    3,
					}
					data_source.Db.Where("id = ?", id).Updates(&orderupdata)
				}
				data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("xingbi", user.Xingbi-orders.Uprice)
				xingbi := models.Userxingbi{
					Uid:     uid,
					Price:   orders.Uprice,
					Status:  2,
					Title:   "约会付款-付对方的",
					Addtime: time.Now().Unix(),
					Dqprice: user.Xingbi - orders.Uprice,
				}
				data_source.Db.Create(&xingbi)
			}
		}
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    orders,
		"msg":     "ok",
	})
	return
}

func Ordersinfo(ctx *gin.Context) {
	request := OrderslistBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	id := request.ID

	var orders models.Orders
	data_source.Db.Where("id = ?", id).First(&orders)
	orders.Fukuanstatus = 1
	if orders.Upriceuid > 0 && orders.Userpriceuid > 0 {
		orders.Fukuanstatus = 2 //都已经付款
	} else if orders.Userpriceuid == uid && orders.Upriceuid != uid {
		orders.Fukuanstatus = 3 //自己已付款
	} else if orders.Userpriceuid != uid && orders.Userpriceuid > 0 {
		orders.Fukuanstatus = 4 //对方已付款
	} else if orders.Upriceuid != uid && orders.Upriceuid > 0 {
		orders.Fukuanstatus = 4 //对方已付款
	}
	var uidarr []int64
	uidarr = append(uidarr, orders.Uid)
	uidarr = append(uidarr, orders.Userid)

	var users []models.Userdingdan
	data_source.Db.Model(&models.User{}).Where("id in (?)", uidarr).Find(&users)
	var hongniang models.Hongniangsql

	var miyao string
	miyao = service.Miyao()
	key := []byte(miyao)

	if orders.Hongniangid > 0 {
		data_source.Db.Where("id = ?", orders.Hongniangid).First(&hongniang)

	}
	orders.Hongniang = hongniang

	for _, u := range users {
		if orders.Uid == u.ID {
			phonejm, _ := service.Encrypt(key, u.Phone)
			u.Phone = phonejm
			orders.U = u
		}
		if orders.Userid == u.ID {
			phonejm, _ := service.Encrypt(key, u.Phone)
			u.Phone = phonejm
			orders.User = u
		}
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    orders,
		"msg":     "ok",
	})
	return
}
func Orderslist(ctx *gin.Context) {
	request := OrderslistBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	classify := request.Classify
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)

	var orderusers []models.Orderuser
	data_source.Db.Where("uid = ?", uid).Find(&orderusers)
	var orderidarr []int64
	for _, o := range orderusers {
		orderidarr = append(orderidarr, o.Orderid)
	}

	var count int64
	var orders []models.Orders
	res := data_source.Db.Where("id in (?)", orderidarr)
	if classify > 0 {
		if classify == 2 {
			res = res.Where("status = 7 or status = 8")
		} else if classify == 3 {
			res = res.Where("status = 2 or status = 6")
		} else if classify == 1 {
			res = res.Where("status = 1  or status = 3  or status = 4")
		}
	}
	res.Model(&models.Orders{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&orders)

	var uidarr []int64
	var hongniangidarr []int64
	for k, v := range orders {
		orders[k].Fukuanstatus = 1 //全部都未付款

		if v.Upriceuid > 0 && v.Userpriceuid > 0 {
			orders[k].Fukuanstatus = 2 //都已经付款
		} else if v.Userpriceuid == uid && v.Upriceuid != uid {
			orders[k].Fukuanstatus = 3 //自己已付款
		} else if v.Userpriceuid != uid && v.Userpriceuid > 0 {
			orders[k].Fukuanstatus = 4 //对方已付款
		} else if v.Upriceuid != uid && v.Upriceuid > 0 {
			orders[k].Fukuanstatus = 4 //对方已付款
		}

		uidarr = append(uidarr, v.Uid)
		uidarr = append(uidarr, v.Userid)
		hongniangidarr = append(hongniangidarr, v.Hongniangid)
	}
	var users []models.Userdingdan
	data_source.Db.Model(&models.User{}).Where("id in (?)", uidarr).Find(&users)
	var hongniangs []models.Hongniangsql
	data_source.Db.Model(&models.Hongniang{}).Where("id in (?)", hongniangidarr).Find(&hongniangs)
	var miyao string
	miyao = service.Miyao()
	key := []byte(miyao)

	for k, o := range orders {
		for _, u := range users {
			if o.Uid == u.ID {
				phonejm, _ := service.Encrypt(key, u.Phone)
				u.Phone = phonejm
				orders[k].U = u
			}
			if o.Userid == u.ID {
				phonejm, _ := service.Encrypt(key, u.Phone)
				u.Phone = phonejm
				orders[k].User = u
			}
		}
		for _, h := range hongniangs {
			if o.Hongniangid == h.ID {
				orders[k].Hongniang = h
			}
		}
	}

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := OrderslistRBody{
		Data:       orders,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}
